<?php

class Feedbacks extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function dispatcher($method='', $para1=NULL, $para2=NULL, $para3=NULL)
    {
        $current_menu        = array('current_menu' => '/');
        $main_content        = NULL;
        $main_content_params = array();
        $lang                = $this->_lang;
        
        $layout = 'layout/content_layout';

        switch ($method)
        {
            case 'send_feedback' :
                $main_content           = $this->feedback();
                break;
            case 'list_feedback' :
                $main_content           = $this->list_feedback(array('page' => $para1));
                break;
        }

        $view_data                      = modules::run('pages/pages/get_genaral_content', array('lang' => $lang));
        $view_data['url']               = get_url_by_lang($lang, 'testimonial');
        $view_data['main_content']      = $main_content;
        $view_data['title']             = $this->_title;
        $view_data['keywords']          = $this->_keywords;
        $view_data['description']       = $this->_description;

        $this->load->view($layout, $view_data);
    }

    function list_feedback($options = array())
    {
        $total_row          = $this->feedbacks_model->get_feedbacks_count($options);
        $total_pages        = (int)($total_row / FRONT_END_FEEDBACK_PER_PAGE);
        if ($total_pages * FRONT_END_FEEDBACK_PER_PAGE < $total_row) $total_pages++;
        if ((int)$options['page'] > $total_pages) $options['page'] = $total_pages;

        $options['offset']  = $options['page'] <= DEFAULT_PAGE ? DEFAULT_OFFSET : ((int)$options['page'] - 1) * FRONT_END_FEEDBACK_PER_PAGE;
        $options['limit']   = FRONT_END_FEEDBACK_PER_PAGE;

        $config = prepare_pagination(
            array(
                'total_rows'    => $total_row,
                'per_page'  => $options['limit'],
                'offset'        => $options['offset'],
                'js_function'   => 'change_page'
            )
        );
        $this->pagination->initialize($config);

        $options['is_admin']                = TRUE;
        $view_data = array();
        $view_data['feedbacks']             = $this->feedbacks_model->get_feedbacks($options);
        $view_data['page']                  = $options['page'];
        $view_data['page_links']            = $this->pagination->create_ajax_links();
        $this->_title                       = __('IP_testimonials') . DEFAULT_TITLE_SUFFIX;
        return $this->load->view('list_feedback',$view_data, TRUE);
        
    }

    function feedback()
    {
        $options = array();

        if ($this->is_postback())
            if (!$this->_send_feedback())
                $options['error'] = $this->_last_message;
            else
                $options['succeed'] = $this->_last_message;

        // prepare page header info.

        $this->_title   = 'Ý kiến khách hàng' . DEFAULT_TITLE_SUFFIX;
        $this->_keywords    = $this->_title . ' ' . $this->_keywords;
        $this->_description = $this->_description;
        // end.

        return $this->load->view('feedback_form', array('options' => $options), TRUE);
    }

    private function _send_feedback()
    {
        $this->_last_message  = '';
        $options                    = array();

        $this->form_validation->set_rules('name', __("IP_fullname"), 'trim|required|xss_clean');
        $this->form_validation->set_rules('content', __("IP_content"), 'trim|required|xss_clean');
        $this->form_validation->set_rules('security_code', __("IP_capcha"), 'trim|required|matches_value[' . $this->phpsession->get('captcha') . ']|xss_clean');

        if ($this->form_validation->run())
        {
            $data = array(
                'customer'      => $this->input->post('name'),
                'content'       => $this->input->post('content'),
                'created_date'  => date('Y-m-d H:i:s'),
                'status'        => INACTIVE_FEEDBACK

                );

            $this->feedbacks_model->add_feedback($data);
            $this->_last_message = '<p>Bạn đã gửi ý kiến phản hồi thành công.</p>';
            return TRUE;
        }
        else
        {
            $this->_last_message = validation_errors();
            return FALSE;
        }
    }
    
    function get_feedbacks_homepage($options = array())
    {
        $view_data              = array();
        $options['limit']       = LIMIT_FEEDBACK_RIGHT_CONTENT;
        $view_data['feedbacks'] = $this->feedbacks_model->get_feedbacks($options);
        $view_data['lang']      = $options['lang'];
        return $this->load->view('feedbacks', $view_data, TRUE);
    }

}
?>